package com.zvezda.android.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.text.format.DateUtils;
import android.util.Log;

/**
 * 时间格式转换
 * 
 * @author Administrator
 * 
 */
public class TimeConversion
{
	/**
	 * 返回几小时前(几分钟前)
	 * @param createTime
	 * @return
	 */
	public static String getClacTime(String createTime)
	{
		long diff = System.currentTimeMillis() - Long.valueOf(createTime) * 1000;// 这样得到的差值是微秒级别
		String timeStr = "";
		// 计算年月日时分秒
		int year = (int) (diff / DateUtils.YEAR_IN_MILLIS);
		int month = (int) (diff / (DateUtils.WEEK_IN_MILLIS * 4));
		int day = (int) (diff / DateUtils.DAY_IN_MILLIS);
		int hour = (int) (diff / DateUtils.HOUR_IN_MILLIS);
		int minute = (int) (diff / DateUtils.MINUTE_IN_MILLIS);
		if (year > 0)
		{
			timeStr = String.valueOf(year) + "年前";
		}
		else if (month > 0)
		{
			timeStr = String.valueOf(month) + "月前";
		}
		else if (day > 0)
		{
			timeStr = String.valueOf(day) + "天前";
		}
		else if (hour > 0)
		{
			timeStr = String.valueOf(hour) + "小时前";
		}
		else if (minute > 0)
		{
			timeStr = String.valueOf(minute) + "分钟前";
		}
		else
		{
			timeStr = "1分钟内";
		}
		return timeStr;
	}
	/**
	 * 日志标签
	 */
	private final static String LOG_TAG = "ZVEZDA";
	/**
	 * 获得时间
	 * @param time
	 * @return
	 */
	public static String getTime(Object time)
	{
		return getTime(time, "yyyy-MM-dd HH:mm:ss");
	}	
	/**
	 * 更具格式获得日期
	 * @param time
	 * @param format
	 * @return
	 */
	public static String getTime(Object time,String format)
	{
		try
		{
			Date date = null;
			if(time instanceof String)
			{
				date = new Date(Integer.parseInt(time.toString()));
			}
			else if (time instanceof Long)
			{
				date = new Date((Long)time);
			}
			else
			{
				return time.toString();
			}
			SimpleDateFormat dateFormat = new SimpleDateFormat(format);
			return dateFormat.format(date);
		} 
		catch (Exception e)
		{
			Log.e(LOG_TAG, "转化时间异常------>"+e.toString());
		}
		return time.toString();
	}
}
